package edu.buffalo.cse.cse486_586.entity;

import java.util.Map;
import java.util.TreeMap;

import android.util.Log;

import edu.buffalo.cse.cse486_586.service.MessageBean;
import edu.buffalo.cse.cse486_586.service.MessageProcessor;
import edu.buffalo.cse.cse486_586.util.AppUtil;

public class PendingMessage implements Message {
	private String messageId;
	private String content;
	private int senderPort;
	private Map<Integer, Integer> vector;
	private boolean foreward;
	private boolean isComparable;
	private VoteMessage finalVm;
	private int status;

	public PendingMessage() {
		super();
		// initiate sender vector
		this.vector = new TreeMap<Integer, Integer>();
		this.finalVm = null;
		this.isComparable = false;
		this.status = AppUtil.UNDECIDED;
		for (int id : AppUtil.PROCESS_ID) {
			this.vector.put(id, 0);
		}
	}

	public PendingMessage(String messageId, String content, int senderPort) {
		super();
		this.messageId = messageId;
		this.content = content;
		this.senderPort = senderPort;
		this.finalVm = null;
		this.foreward = false;
		this.isComparable = false;
		this.status = AppUtil.UNDECIDED;
		// initiate sender vector
		this.vector = new TreeMap<Integer, Integer>();
		for (int id : AppUtil.PROCESS_ID) {
			this.vector.put(id, 0);
		}
	}

	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Map<Integer, Integer> getVector() {
		return vector;
	}

	public void setVector(Map<Integer, Integer> vector) {
		this.vector = vector;
	}

	public int getSenderPort() {
		return senderPort;
	}

	public void setSenderPort(int senderPort) {
		this.senderPort = senderPort;
	}

	public boolean getForeward() {
		return foreward;
	}

	public void setForeward(boolean foreward) {
		this.foreward = foreward;
	}
	
	public VoteMessage getFinalVm() {
		return finalVm;
	}

	public void setFinalVm(VoteMessage finalVm) {
		this.finalVm = finalVm;
	}
	
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}
	
	public boolean isComparable() {
		return isComparable;
	}
	
	public void setComparable(boolean isComparable) {
		this.isComparable = isComparable;
	}

	@Override
	public String toString() {
		// Example: 1/--/5554_1/--/0,0,0,0,1/--/Hello world
		String vectorStr = MessageBean.getInstance().getLocalVectorString();
		StringBuilder str = new StringBuilder();

		if (this.foreward) {
			str.append(MessageBean.FOREWARD_MESSAGE).append(MessageProcessor.MESSAGE_SPLITTER).append(messageId)
			.append(MessageProcessor.MESSAGE_SPLITTER).append(vectorStr)
			.append(MessageProcessor.MESSAGE_SPLITTER).append(content);
		} else {
			str.append(MessageBean.PENDING_MESSAGE).append(MessageProcessor.MESSAGE_SPLITTER).append(messageId)
			.append(MessageProcessor.MESSAGE_SPLITTER).append(vectorStr)
			.append(MessageProcessor.MESSAGE_SPLITTER).append(content);
		}
		Log.d("PendingMessage", str.toString());
		return str.toString();
	}
}
